package com.southgis.ibase.sql.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

/**
 * 数据源配置相关常量
 * @author dennis
 *
 */
public final class DataSourceConst
{
	/** 如配置文件中未指定数据源实现类型，使用该默认值 */
	public static final Object DATASOURCE_TYPE_DEFAULT = "com.zaxxer.hikari.HikariDataSource";

	/** 默认数据源配置Bean名称 */
	public static final String DEFAULT_DATASOURCE_PROPERTIES_BEAN_NAME = "dataSourceProperties";
	/** 默认Jpa配置Bean名称 */
	public static final String DEFAULT_JPA_PROPERTIES_BEAN_NAME = "jpaProperties";
	/** 默认Jpa提供者Hibernate相关配置Bean名称 */
	public static final String DEFAULT_JPA_HIBERNATE_BEAN_NAME = "jpaHibernate";

	/** 默认数据源Bean名称 */
	public static final String DEFAULT_DATASOURCE_BEAN_NAME = "dataSource";

	/** 默认实体管理工厂Bean名称 */
	public static final String DEFAULT_ENTITY_FACTORY_BEAN_NAME = "entityManagerFactory";

	/** 默认实体管理Bean名称 及jpa持久化单元名（persistenceUnit） */
	public static final String DEFAULT_ENTITY_MANAGER_BEAN_NAME = "entityManager";

	/** 默认事务管理Bean名称 */
	public static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";

	/** 动态数据源Bean名称 */
	public static final String DYNAMIC_DATASOURCE_BEAN_NAME = "dynamicDataSource";
	
	/** 动态数据源的NamedParameterJdbcTemplate */
	public static final String DYNAMIC_NAMED_JDBCTEMPLATE_BEAN_NAME = "dynamicNamedJdbcTemplate";

	/** 动态数据源事务管理Bean名  */
	public static final String DYNAMIC_TRANSACTION_MANAGER_BEAN_NAME = "dynamicTransactionManager";

	/**
	 * 根据配置设置JPA提供商配置项，参照HibernateJpaVendorAdapter及HibernateProperties的处理。<br>
	 * 相同配置的优先级：jpaProperties.getProperties() &gt; jpaHibernate &gt; jpaProperties其他属性(除properties外)
	 * @param jpaProperties jpa规范属性（包含properties为提供商特有属性）
	 * @param jpaHibernate Spring封装的提供商特有属性，可为null。
	 * @return
	 */
	public static Map<String, Object> getVendorProperties(JpaProperties jpaProperties,HibernateProperties jpaHibernate)
	{
		//高优先级jpa.properties
		Map<String, Object> prop=new HashMap<String, Object>(jpaProperties.getProperties());
		
		//中优先级jpa.hibernate
		if(jpaHibernate!=null) {
			if(!prop.containsKey(AvailableSettings.HBM2DDL_AUTO)) {
				if(!StringUtils.isEmpty(jpaHibernate.getDdlAuto())) {
					prop.put(AvailableSettings.HBM2DDL_AUTO, jpaHibernate.getDdlAuto());
				}
			}
			if(!prop.containsKey(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS)) {
				if(jpaHibernate.isUseNewIdGeneratorMappings()!=null) {
					prop.put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
							jpaHibernate.isUseNewIdGeneratorMappings().toString());
				}else {
					prop.put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
				}
			}
			
			//下述属性暂未处理
			//jpaHibernate.getNaming();//表字段命名规则
		}
		
		//低优先级 jpa.xxx
		if(!prop.containsKey(AvailableSettings.HBM2DDL_AUTO)) {
			if(jpaProperties.isGenerateDdl()) {
				prop.put(AvailableSettings.HBM2DDL_AUTO, "update");
			}
		}
		if(!prop.containsKey(AvailableSettings.SHOW_SQL)) {
			if (jpaProperties.isShowSql()) {
				prop.put(AvailableSettings.SHOW_SQL, "true");
			}
		}
		if(!prop.containsKey(AvailableSettings.DIALECT)) {
			if(!StringUtils.isEmpty(jpaProperties.getDatabasePlatform())) {
				prop.put(AvailableSettings.DIALECT, jpaProperties.getDatabasePlatform());
			}
		}
		//下述属性暂未处理
		//jpaProperties.getMappingResources();
		//jpaProperties.getDatabase();
		//jpaProperties.getOpenInView();

		return prop;
	}
}
